package com.ssm.controller;

import com.ssm.domain.Permission;
import com.ssm.domain.Role;
import com.ssm.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @GetMapping("/findAll")
    public ModelAndView findAll(){
       List<Role> list =  roleService.findAll();
        ModelAndView mv = new ModelAndView();
        mv.addObject("roleList", list);
        mv.setViewName("role-list");
        return mv;
    }

    @PostMapping("/save")
    public String save(Role role){
        roleService.save(role);
        return "redirect:findAll";
    }

    @GetMapping("/findRoleByIdAndAllPermission/{id}")
    public ModelAndView findRoleByIdAndAllPermission(@PathVariable String id){
        ModelAndView mv = new ModelAndView();
        Role role =  roleService.findById(id);
        List<Permission> list = roleService.findOtherPerm(id);
        mv.addObject("role", role);
        mv.addObject("permissionList", list);
        mv.setViewName("role-permission-add");
        return mv;
    }
    @PostMapping("/addPermToRole/{id}")
    public String addPermToRole(@PathVariable String id,@RequestParam List<String> ids){
//        System.out.println(ids.getClass());//class java.util.ArrayList
        roleService.addPermToRole(id,ids);
        return "redirect:../findAll";
    }

    /**
     * 待完成
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    public ModelAndView findById(@PathVariable String id){
        ModelAndView mv = new ModelAndView();



        return mv;
    }

}
